<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Cache;
use App\Facades\DeviceService;

class DeviceRecord extends Model
{
    public $fillable = [
        'device_id',
        'TDS1',
        'TDS2',
        'message_number',
        'dec_status',
        'filter_cnt1',
        'filter_cnt2',
        'filter_cnt3',
        'filter_cnt4',
        'filter_cnt5',
        'filter_set1',
        'filter_set2',
        'filter_set3',
        'filter_set4',
        'filter_set5',
        'filter1',
        'filter2',
        'filter3',
        'filter4',
        'filter5',
        'total_flow',
        'residual_flow',
        'filter_status',
        'flow_status',
        'time_limit',
        'amount_water',
        ];

    public function device()
    {
        return $this->belongsTo('App\Models\Device', 'device_id', 'id');
    }

    //缺水
    const HYDROPENIA = 2;
    //检修
    const OVERHAUL = 4;
    //漏水
    const LEAKAGE = 8;
    //停机
    const HALT = 9;

    protected $appends = [
        'device_status_description',
    ];

    public static $_DEVICE_STATUS_DESCRIPTION = [
        1 => '正常',
        self::HYDROPENIA => '缺水',
        3 => '正常',
        self::OVERHAUL => '检修',
        5 => '正常',
        6 => '欠费',
        7 => '正常',
        self::LEAKAGE => '漏水',
        self::HALT => '停机',
    ];

    public function getDecStatusAttribute()
    {
        $device = DeviceService::getDeviceCodeByid($this->attributes['device_id']);

        $device_status = Cache::get($device . 'state');

        return $device_status['decStatus'];
    }

    // 
    public function getDeviceStatusDescriptionAttribute()
    {
        
        $device = DeviceService::getDeviceCodeByid($this->attributes['device_id']);

        $device_status = Cache::get($device . 'state');

        $status = $device_status['decStatus'];

        switch ($status) {
            case '1':
                return self::$_DEVICE_STATUS_DESCRIPTION[1];
                break;
            case '2':
                return self::$_DEVICE_STATUS_DESCRIPTION[2];
                break;
            case '3':
                return self::$_DEVICE_STATUS_DESCRIPTION[3];
                break;
            case '4':
                return self::$_DEVICE_STATUS_DESCRIPTION[4];
                break;
            case '5':
                return self::$_DEVICE_STATUS_DESCRIPTION[5];
                break;
            case '6':
                return self::$_DEVICE_STATUS_DESCRIPTION[6];
                break;
            case '7':
                return self::$_DEVICE_STATUS_DESCRIPTION[7];
                break;
            case '8':
                return self::$_DEVICE_STATUS_DESCRIPTION[8];
                break;
            case '9':
                return self::$_DEVICE_STATUS_DESCRIPTION[9];
                break;
            default:
                # code...
                break;
        }

         return self::$_DEVICE_STATUS_DESCRIPTION[$this->attributes['dec_status']];
    }

}
